package javabasic.threads;

public class DiffNotifyYield {
    public static void main(String[] args) {
        //如果调用了wait方法且没有指定等待时间，并且没有使用notify方法唤醒，则该线程会永远等待
        Object lock = new Object();
        new Thread(new Runnable() {
            @Override
            public void run() {
                System.out.println("线程A等待获取锁。。。");
                synchronized (lock){
                    System.out.println("线程A获取到了锁");
                    System.out.println("线程A执行业务逻辑");
                    try {
                        Thread.sleep(20);
                        System.out.println("线程A开始执行wait方法");
                        lock.wait();
                        System.out.println("线程A执行完毕");
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        }).start();
        try {
            Thread.sleep(10);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        new Thread(new Runnable() {
            @Override
            public void run() {
                System.out.println("线程B尝试获取锁");
                synchronized (lock){
                    System.out.println("线程B获取到了锁");
                    System.out.println("线程B执行完毕");
                    //线程B唤醒线程A继续执行，如果不调用notify方法，则线程A会一直等待
                    lock.notify();
                }
            }
        }).start();
    }
}
